
clear all
set more off

use "MPPEC Dataset.dta"

gen id = _n
preserve

bysort pair: gen diff=priority1_funded[2]-priority1_funded[1] if _n==1

do hodges.do

restore
merge 1:1 id using treatment_comb



*** Fisher's randomization experiment

*Keele et al - subtract k from all treatment outcomes of interest for various k

*********************************************
*DIFFERENCE IN MEANS
*********************************************
*foreach num of numlist -1.5(.005)1  {
*preserve
*replace priority1_funded = priority1_funded -`num'  if mobilization == 1
	sort pair mobilization
	capture drop diff
	bysort pair: gen diff=priority1_funded[2]-priority1_funded[1] if _n==1
	egen avg_diff_0=mean(diff) 
	drop diff

	
	
**************************************************************************
***                     Within pairs re-assignment of treatment
**************************************************************************

*preserve


local nsim=128                                                   // define the number of simulations
qui forvalues i=1/`nsim' {
	***    Running baseline analysis (difference bw treatment and control)
	**************************************************************************
	
	sort pair mobilization_`i'
	bysort pair: gen diff_`i'=priority1_funded[2]-priority1_funded[1] if _n==1
	egen avg_diff_`i'=mean(diff_`i')
	drop diff_`i' mobilization_`i'

}
***
qui {
	***    Manipulating the output from the simulations
	**************************************************************************
	
	drop if _n>1
	keep avg_diff_*
	gen id=_n-1
	
	reshape long avg_diff_, i(id) j(bsrep)
	ren avg_diff_ avg_diff_sim
	drop id
	gen avg_diff=avg_diff_sim[1]
	drop if bsrep==0
	
	sort avg_diff_sim
	local ci1 =  avg_diff_sim[round(0.025*(_N),1)]
	local ci2 = avg_diff_sim[round(0.975*(_N),1)]
	local sign=sign(avg_diff[1])
	noi di `num'
	if `sign'>0 {
		
		qui count if avg_diff_sim>avg_diff
		local greater=r(N)
		
		
		*** P-value calculation for one-tailed hypothesis tests
				
		noi di "The p-value of the one-tailed test of the null hypothesis that the mean difference between Treatment and Control is 0 is:  " `=`greater'/_N'
		
}
	
	else {

		qui count if avg_diff_sim<avg_diff
		local smaller=r(N)
		
		
		
		*** P-value calculation for one-tailed hypothesis tests
			
		noi di "The p-value of the one-tailed test of the null hypothesis that the mean difference between Treatment and Control is 0 is:  " `=`smaller'/_N'
		
		}
	}

restore

}


************************************************
*RANK SUM
************************************************

clear all
set more off

cd "C:\Users\Aletheia\Dropbox\Ryan\Kenya Local Gov Experiment\Data Archive"

use "Kenya Local Gov-August.dta"

gen id = _n

merge 1:1 id using treatment_comb
assert _merge == 3
drop _merge

*** Creating Wilcoxon Signed Rank test statistic for actual data
*foreach num of numlist -1.5(.005)1.5  {
*preserve
*replace priority1_funded = priority1_funded -`num'  if mobilization == 1
sort pair mobilization
bysort pair: gen diff=priority1_funded[2]-priority1_funded[1] if _n==1
gen abs_diff = abs(diff)
egen rank = rank(abs_diff) if abs_diff != .
egen w_0 = sum(rank) if diff > 0
sum w_0
replace w_0 = r(mean) if w_0 == .
drop diff-rank
	
**************************************************************************
***                     Within pairs re-assignment of treatment
**************************************************************************

* Creating Wilcoxon Signed Rank test statistic for simulated data

local nsim=128 
                                                 // define the number of simulations
qui forvalues i=1/`nsim' {
	***    Running baseline analysis (difference bw treatment and control)
	**************************************************************************
	
	sort pair mobilization_`i'
	bysort pair: gen diff_`i' =priority1_funded[2]-priority1_funded[1] if _n==1
	gen abs_diff_`i' = abs(diff_`i')
	egen rank_`i' = rank(abs_diff_`i') if abs_diff_`i' != .
	egen w_`i' = sum(rank_`i') if diff_`i' > 0
	sum w_`i'
	replace w_`i' = r(mean) if w_`i' == .
}

*qui {
drop if _n>1
	keep w_*
	gen id=_n-1
	
	reshape long w_, i(id) j(bsrep)
	ren w_ w_sim
	drop id
	gen w=w_sim[1]
	drop if bsrep==0
	sort w_sim
	
	gen z_sim = (w_sim - 14)/5.916
	gen z = (w - 14)/5.916
	sort z_sim
	local ci1 =  w_sim[round(0.025*(_N),1)]
	local ci2 = w_sim[round(0.975*(_N),1)]
	local ci1_b =  z_sim[round(0.025*(_N),1)]
	local ci2_b = z_sim[round(0.975*(_N),1)]
	local sign=sign(z)
noi di `num'
	if `sign'>0 {
		
		qui count if z_sim>z
		local greater=r(N)
		
		
		*** P-value calculation for one-tailed hypothesis tests
				
		noi di "The p-value of the one-tailed test of the null hypothesis that the mean difference between Treatment and Control is 0 is:  " `=`greater'/_N'
		
}
	
	else {

		qui count if z_sim<z
		local smaller=r(N)
		
		
		
		*** P-value calculation for one-tailed hypothesis tests
			
		noi di "The p-value of the one-tailed test of the null hypothesis that the mean difference between Treatment and Control is 0 is:  " `=`smaller'/_N'
		
		}

restore
}

************************************************
*MC NEMARS
************************************************

clear all
set more off

cd "C:\Users\Aletheia\Dropbox\Ryan\Kenya Local Gov Experiment\Data Archive"

use "Kenya Local Gov-August.dta"

gen id = _n

merge 1:1 id using treatment_comb
assert _merge == 3
drop _merge

sum priority1_funded if mobilization == 1

gen mcnemar_0= r(sum)


local nsim=128                                                   // define the number of simulations
qui forvalues i=1/`nsim' {
	***    Running baseline analysis (difference bw treatment and control)
	**************************************************************************

	sum priority1_funded if mobilization_`i' == 1

	gen mcnemar_`i'= r(sum)
	
	drop mobilization_`i'

}


qui {
	***    Manipulating the output from the simulations
	**************************************************************************
	
	drop if _n>1
	keep mcnemar_*
	gen id=_n-1
	
	reshape long mcnemar_, i(id) j(bsrep)
	ren mcnemar_ mcnemar_sim
	drop id
	gen mcnemar =mcnemar_sim[1]
	drop if bsrep==0
	
	sort mcnemar_sim

		
		qui count if mcnemar_sim> mcnemar
		local greater=r(N)
		
		
		*** P-value calculation for one-tailed hypothesis tests
				
		noi di "The p-value of the one-tailed test is:  " `=`greater'/_N'
		
}
	
